var PRO = PRO || {};
PRO.customerUnit = (function() {
    function cutomCheckbox(){
        var items = $(".checkbox-icons");
        //根据checkbox初始化选择状态
        for (var i = 0; i < items.length; i++) {
            var checkbox = $(items[i]).find("input[type=checkbox]");
            if (checkbox.is(':checked')) {
                $(items[i]).addClass('checkbox-icons-checked');
            }
        }
        $("body").on("click",".checkbox-icons input[type=checkbox]",function(){
            var that = $(this);
            if (that.is(':checked')) {
                that.parent().addClass('checkbox-icons-checked');
                that.parent().parent().addClass('tr-checked');
            }else{
                that.parent().removeClass('checkbox-icons-checked');
                that.parent().parent().removeClass('tr-checked');
            }
        });
    }
    function cutomRadio(){
        var items = $(".radio-icons");
        //根据radio默认checked初始化选择状态
        for (var i = 0; i < items.length; i++) {
            var radio = $(items[i]).find("input[type=radio]");
            if (radio.is(':checked')) {
                if($(items[i]).hasClass('radio-disable-null') || $(items[i]).hasClass('radio-disable')){
                    continue;
                }
                $(items[i]).addClass('radio-icons-checked');
            }
        }
        $("body").on("click",".radio-icons input[type=radio]",function(){
            var that = $(this);
            var name = $(this).attr("name");
            if(that.parent().hasClass('radio-disable-null') || that.parent().hasClass('radio-disable')){
                return;
            }
            if (that.is(':checked')) {
                $("[name='"+name+"']").parent().removeClass('radio-icons-checked');
                that.parent().addClass('radio-icons-checked');
            }
        });
    }
    //选中radio
    function checkRadio(radios){
        for (var i = 0; i < radios.length; i++) {
            var radio = $(radios[i]);
            radio.prop("checked",true);
            var name = radio.attr("name");
            $("[name='"+name+"']").parent().removeClass('radio-icons-checked');
            radio.parent().addClass('radio-icons-checked');
        };
    }
    //取消选中
    function cancelCheckRadio(radios){
        for (var i = 0; i < radios.length; i++) {
            var radio = $(radios[i]);
            radio.prop("checked",false);
            radio.parent().removeClass('radio-icons-checked');
        };
    }
    //checkbox全选
    function checkAll(){
        $("body").on("click",".checkbox-icons-all input[type=checkbox]",function(){
            var that = $(this);
            var table = $(this).closest('table');
            if (that.is(':checked')) {
                that.parent().addClass('checkbox-icons-all-checked');
                table.find('tbody input[type=checkbox]').prop('checked', true);
                table.find('tbody input[type=checkbox]').parent().removeClass('checkbox-icons-checked');
                table.find('tbody input[type=checkbox]').parent().addClass('checkbox-icons-checked');
                table.find('tbody tr').removeClass('tr-checked');
                table.find('tbody tr').addClass('tr-checked');
            }else{
                that.parent().removeClass('checkbox-icons-all-checked');
                table.find('tbody input[type=checkbox]').prop('checked', false);
                table.find('tbody input[type=checkbox]').parent().removeClass('checkbox-icons-checked');
                table.find('tbody tr').removeClass('tr-checked');
            }
        });
    }
    function changeTheTab(){
        var lis = $(".tab-title li");
        var divs = $(".tab-content");
        if (lis.length!=divs.length) {return;}
        var tabIndex = 0;
        for (var i = 0; i < lis.length; i++) {
            $(lis[i]).attr("id",i);
            $(lis[i]).click(function() {
                var that = $(this);
                tabIndex = that.attr("id");
                for (var j = 0; j < lis.length; j++) {
                    $(lis[j]).removeClass("tab-title-selected");
                    $(divs[j]).css("display","none");
                }
                $(lis[tabIndex]).addClass('tab-title-selected');
                $(divs[tabIndex]).css("display","");
            });
        }
    }
    //获取选中的checkbox的value
    function getCheckedbox(container,name){
        var checkboxes = [];
        if (name != undefined) {
            container.find("input[type=checkbox][name='"+name+"']:checked").each(function(index,el){
                var value = $(el).val();
                if (value.length > 0) {
                    checkboxes.push(value);
                };

            });
        }else{
            container.find("input[type=checkbox]:checked").each(function(index,el){
                var value = $(el).val();
                if (value.length > 0) {
                    checkboxes.push(value);
                };
            });
        }
        return checkboxes;
    }
    //滚动条
    function createScrollBar(container){
        container.mCustomScrollbar({
            theme:"dark",
            scrollEasing:"linear",
            scrollInertia:300
        });
    }
    function createScrollBarX(container){
        container.mCustomScrollbar({
            theme: "dark",
            axis: "x",
            scrollEasing:"linear",
            scrollInertia:300
        });
    }
    //下拉菜单
    function createChosenList(width,container) {
        container.chosen({
            width : width,
            disable_search : true
        });
    }
    function createChosenListAuto(container) {
        container.chosen({
            disable_search : true
        });
    }
    //选择框checkbox
    function bindChoseCheckboxEvent(){
        $("body").on("click",".choseCheckbox",function(){
            if ($(this).is(":checked")) {
                $(this).parent().addClass('item-checked');
            }else{
                $(this).parent().removeClass('item-checked');
            }
        });
        $("body").on("click",".dialog-choseall",function(){
            var btnStatus = $(this).data("status");
            if (btnStatus == "cancelAll") {
                $(this).parents(".dialog-chose-section").find('.dialog-chose-item').removeClass('item-checked').find(".choseCheckbox").prop('checked', false);
                $(this).text("全选").data("status","choseall").removeClass("btn-gray").addClass("btn-blue");
            }else{
                $(this).parents(".dialog-chose-section").find(".choseCheckbox").each(function(index,el){
                    var item = $(el);
                    if ((!item.is(':checked'))&& (!item.parent().hasClass("hide"))) {
                        //console.log("123");
                        $(el).prop('checked', true).parent().addClass('item-checked');
                    }
                });
                $(this).text("取消全选").data("status","cancelAll").removeClass("btn-blue").addClass("btn-gray");
            }

        });
    }
    //选择框radio
    function bindChoseRadioEvent(){
        $("body").on("click",".choseRadio",function(){
            if ($(this).is(":checked")) {
                var name = $(this).attr('name');
                $("[name='"+name+"']").parent().removeClass('item-checked');
                $(this).parent().addClass('item-checked');
            }
        });
    }
    //获取选中的单选框
    function getCheckedRadio(container,name){
        var checkedRadio = {};
        var radio = container.find("input[type=radio][name='"+name+"']:checked");
        checkedRadio.value = radio.val();
        checkedRadio.text = radio.parent().find('span').text();
        return checkedRadio;
    }
    //选中某个单选框
    function checkRadioItem(radios){
        for (var i = 0; i < radios.length; i++) {
            var radio = $(radios[i]);
            radio.prop("checked",true);
            var name = radio.attr("name");
            $("[name='"+name+"']").parent().removeClass('item-checked');
            radio.parent().addClass('item-checked');
        };
    }
    //取消全选
    function cancelChoseAll(container){
        container.find('.choseCheckbox').prop('checked', false).parent().removeClass('item-checked');
    }
    // 取消选中的checkbox
    function cancelCheckedCheckbox(container,ids){
        for (var i = 0; i < ids.length; i++) {
            container.find('input[type=checkbox][value="'+ids[i]+'"]').prop('checked', false).parent().removeClass('item-checked');
        };
    }
    // 取消选中的checkbox
    function cancelCheckedCheckbox2(container,ids){
        for (var i = 0; i < ids.length; i++) {
            container.find('input[type=checkbox][value="'+ids[i]+'"]').prop('checked', false).parent().removeClass('checkbox-icons-checked');
        };
    }
    //选中指定checkbox
    function checkedCheckbox(container,ids){
        for (var i = 0; i < ids.length; i++) {
            container.find('input[type=checkbox][value="'+ids[i]+'"]').prop('checked', true).parent().addClass('item-checked');
        };
    }
    //保存弹窗index
    function saveLayerIndex(index,save,cancel){
        $(save).attr("dialog-index",index);
        $(cancel).attr('dialog-index', index).click(function(){
            layer.close($(this).attr('dialog-index'));
        });
        $('.xubox_close').focus();
    }
    //获取某个容器下选中的checkbox的值，返回string数组
    function getCheckboxValue(container){
        var value = [];
        container.find('input[type=checkbox]').each(function(index, el) {
            if ($(el).is(':checked')) {
                var temp = {
                    value: $(el).val(),
                    icon:$(el).attr("data-icon"),
                    text: $(el).parent().find('span').text()
                }
                value.push(temp);
            };
        });
        return value;
    }
    //绑定弹窗搜索事件
    function bindDialogScreenEvent(){
        //弹窗页面搜索
        $("body").on("click",".dialog-search",function(){
            var searchText = $(this).prev('.input-text-search').val();

            //取消全选
            var parent = $(this).parents(".dialog-chose-section");
            var itemlist = parent.find(".dialog-chose-item");
            //YOP.customerUnit.cancelChoseAll(parent.find(".dialog-chose-list"));
            if (searchText.length == 0) {
                itemlist.removeClass("hide");
                return;
            };
            itemlist.removeClass("hide").each(function(index, el) {
                var text = $(el).text();
                var regExp = new RegExp(searchText, 'g');
                if(!regExp.test(text)){
                    $(el).addClass('hide');
                }
            });
            $(this).parents(".dialog-title").find(".input-select").val("opstart").trigger("chosen:updated");
            layer.autoArea($(this).parents(".addnew-dialog-nopadding").find(".btn-save").attr("dialog-index"));
        });
        $("body").on("keypress",".dialog-search",function(e){
            if (e.which == 13) {
                return false;
            }
        });
        //下拉筛选
        $("body").on("change",".dialog-select-dpt",function(){
            var value = $(this).val();
            var parent = $(this).parents(".dialog-chose-section");
            var itemlist = parent.find(".dialog-chose-item");
            //YOP.customerUnit.cancelChoseAll(parent.find(".dialog-chose-list"));
            if (value == "opstart") {return;};
            if(value == "showall"){
                itemlist.removeClass("hide");
            }else{
                itemlist.removeClass("hide");
                itemlist.each(function(index, el) {
                    if (!($(el).attr("data-dptid")==value)) {
                        $(el).addClass("hide");
                    }
                });
            }
            layer.autoArea($(this).parents(".addnew-dialog-nopadding").find(".btn-save").attr("dialog-index"));
        });
    }
    //创建日期+时间
    function createDateTime(){
        var myDate = new Date();
        var curYear=myDate.getFullYear();
        var beforYear=curYear-120;
        var nextYear=curYear+100;
        $('.datetimepicker').datetimepicker({
            dateFormat:'yy-mm-dd',
            timeFormat: "hh:mm",
            changeYear:true,
            changeMonth:true,
            //minDate:new Date(),
            yearRange: beforYear+":"+nextYear,
            monthNamesShort:['01','02','03','04','05','06','07','08','09','10','11','12'],
            monthNames:['01','02','03','04','05','06','07','08','09','10','11','12']

        });
    }
    //创建日历
    function createCalendar(){
        var myDate = new Date();
        var curYear=myDate.getFullYear();
        var beforYear=curYear-120;
        var nextYear=curYear+100;
        $('.datepicker').datepicker({
            dateFormat:'yy-mm-dd',
            changeYear:true,
            changeMonth:true,
            yearRange: beforYear+":"+nextYear,
            monthNamesShort:['01','02','03','04','05','06','07','08','09','10','11','12'],
            monthNames:['01','02','03','04','05','06','07','08','09','10','11','12']

        });
    }
    function createDialogCalendar(){
        var dialogCalendar = $(this).find('.datepicker');
        // console.log(dialogCalendar);
        if (dialogCalendar.length > 0) {
            //日历
            var myDate = new Date();
            var curYear=myDate.getFullYear();
            var beforYear=curYear-120;
            var nextYear=curYear+100;
            dialogCalendar.datepicker({
                dateFormat:'yy-mm-dd',
                changeYear:true,
                changeMonth:true,
                yearRange: beforYear+":"+nextYear,
                monthNamesShort:['01','02','03','04','05','06','07','08','09','10','11','12'],
                monthNames:['01','02','03','04','05','06','07','08','09','10','11','12']

            });
        }
    }
    // 创建开始时间，结束时间一年内的日历
    function createCalendarWidthRange(startDateTextBox,endDateTextBox){
        startDateTextBox.datepicker({
            dateFormat:'yy-mm-dd',
            changeYear:true,
            changeMonth:true,
            monthNamesShort:['01','02','03','04','05','06','07','08','09','10','11','12'],
            monthNames:['01','02','03','04','05','06','07','08','09','10','11','12'],
            onClose: function(dateText, inst) {
                if (endDateTextBox.val() != '') {
                    var testStartDate = startDateTextBox.datetimepicker('getDate');
                    var testEndDate = endDateTextBox.datetimepicker('getDate');
                    if (testStartDate > testEndDate)
                        endDateTextBox.datetimepicker('setDate', testStartDate);
                }
                // else {
                // 	endDateTextBox.val(dateText);
                // }
                startDateTextBox.trigger('dateChanged');
            },
            onSelect: function (selectedDateTime){
                var startDate = startDateTextBox.datetimepicker('getDate');
                var endMax = new Date(startDate.getFullYear()+1,startDate.getMonth(),startDate.getDate(),0,0,0);
                endDateTextBox.datetimepicker('option', 'minDate', startDateTextBox.datetimepicker('getDate') );
                endDateTextBox.datetimepicker('option', 'maxDate', endMax );
            }
        });
        endDateTextBox.datepicker({
            dateFormat:'yy-mm-dd',
            changeYear:true,
            changeMonth:true,
            monthNamesShort:['01','02','03','04','05','06','07','08','09','10','11','12'],
            monthNames:['01','02','03','04','05','06','07','08','09','10','11','12'],
            onClose: function(dateText, inst) {
                if (startDateTextBox.val() != '') {
                    var testStartDate = startDateTextBox.datetimepicker('getDate');
                    var testEndDate = endDateTextBox.datetimepicker('getDate');
                    if (testStartDate > testEndDate)
                        startDateTextBox.datetimepicker('setDate', testEndDate);
                }
                // else {
                // 	startDateTextBox.val(dateText);
                // }
                startDateTextBox.trigger('dateChanged');
            },
            onSelect: function (selectedDateTime){
                var endDate = endDateTextBox.datetimepicker('getDate');
                var endMin = new Date(endDate.getFullYear()-1,endDate.getMonth(),endDate.getDate(),0,0,0);
                startDateTextBox.datetimepicker('option', 'maxDate', endDateTextBox.datetimepicker('getDate') );
                startDateTextBox.datetimepicker('option', 'minDate', endMin );
            }
        });
    }
    // 创建开始时间，结束时间一年内的日历
    function createCalendarWidthRangeDate(startDateTextBox,endDateTextBox){
        startDateTextBox.datepicker({
            dateFormat:'yy-mm-dd',
            changeYear:true,
            changeMonth:true,
            monthNamesShort:['01','02','03','04','05','06','07','08','09','10','11','12'],
            monthNames:['01','02','03','04','05','06','07','08','09','10','11','12'],
            onClose: function(dateText, inst) {
                if (endDateTextBox.val() != '') {
                    var testStartDate = startDateTextBox.datetimepicker('getDate');
                    var testEndDate = endDateTextBox.datetimepicker('getDate');
                    if (testStartDate > testEndDate)
                        endDateTextBox.datetimepicker('setDate', testStartDate);
                }
                // else {
                // 	endDateTextBox.val(dateText);
                // }
            },
            onSelect: function (selectedDateTime){
                endDateTextBox.datetimepicker('option', 'minDate', startDateTextBox.datetimepicker('getDate') );
            }
        });
        endDateTextBox.datepicker({
            dateFormat:'yy-mm-dd',
            changeYear:true,
            changeMonth:true,
            monthNamesShort:['01','02','03','04','05','06','07','08','09','10','11','12'],
            monthNames:['01','02','03','04','05','06','07','08','09','10','11','12'],
            onClose: function(dateText, inst) {
                if (startDateTextBox.val() != '') {
                    var testStartDate = startDateTextBox.datetimepicker('getDate');
                    var testEndDate = endDateTextBox.datetimepicker('getDate');
                    if (testStartDate > testEndDate)
                        startDateTextBox.datetimepicker('setDate', testEndDate);
                }
                // else {
                // 	startDateTextBox.val(dateText);
                // }
            },
            onSelect: function (selectedDateTime){
                startDateTextBox.datetimepicker('option', 'maxDate', endDateTextBox.datetimepicker('getDate') );
            }
        });
    }
    // 创建开始时间，结束时间带时间的日历
    function createCalendarWidthRangeTime(startDateTextBox,endDateTextBox){
        startDateTextBox.datetimepicker({
            dateFormat:'yy-mm-dd',
            changeYear:true,
            changeMonth:true,
            //minDate:new Date(),
            monthNamesShort:['01','02','03','04','05','06','07','08','09','10','11','12'],
            monthNames:['01','02','03','04','05','06','07','08','09','10','11','12'],
            onClose: function(dateText, inst) {
                if (endDateTextBox.val() != '' && startDateTextBox.val() != '') {
                    var testStartDate = startDateTextBox.datetimepicker('getDate');
                    var testEndDate = endDateTextBox.datetimepicker('getDate');
                    if (testStartDate > testEndDate){
                        //endDateTextBox.datetimepicker('setDate', testStartDate);
                        tips("活动开始时间不能大于活动结束时间！",0);
                    }
                }
                // else {
                // 	endDateTextBox.val(dateText);
                // }
            },
            onSelect: function (selectedDateTime){
                endDateTextBox.datetimepicker('option', 'minDate', startDateTextBox.datetimepicker('getDate') );
            }
        });
        endDateTextBox.datetimepicker({
            dateFormat:'yy-mm-dd',
            changeYear:true,
            changeMonth:true,
            //minDate:new Date(),
            monthNamesShort:['01','02','03','04','05','06','07','08','09','10','11','12'],
            monthNames:['01','02','03','04','05','06','07','08','09','10','11','12'],
            onClose: function(dateText, inst) {
                if (startDateTextBox.val() != '' && endDateTextBox.val() != '' ) {
                    var testStartDate = startDateTextBox.datetimepicker('getDate');
                    var testEndDate = endDateTextBox.datetimepicker('getDate');
                    if (testStartDate > testEndDate){
                        //startDateTextBox.datetimepicker('setDate', testEndDate);
                        tips("活动开始时间不能大于活动结束时间！",0);
                    }
                }
                // else {
                // 	startDateTextBox.val(dateText);
                // }
            },
            onSelect: function (selectedDateTime){
                startDateTextBox.datetimepicker('option', 'maxDate', endDateTextBox.datetimepicker('getDate') );
            }
        });
    }
    return {
        cutomCheckbox: cutomCheckbox,
        cutomRadio: cutomRadio,
        checkRadio: checkRadio,
        cancelCheckRadio: cancelCheckRadio,
        checkAll: checkAll,
        changeTheTab: changeTheTab,
        getCheckedbox: getCheckedbox,
        createScrollBar: createScrollBar,
        createScrollBarX: createScrollBarX,
        createChosenList: createChosenList,
        createChosenListAuto:createChosenListAuto,
        bindChoseCheckboxEvent: bindChoseCheckboxEvent,
        saveLayerIndex: saveLayerIndex,
        getCheckboxValue: getCheckboxValue,
        cancelChoseAll: cancelChoseAll,
        bindDialogScreenEvent: bindDialogScreenEvent,
        bindChoseRadioEvent:bindChoseRadioEvent,
        checkedCheckbox:checkedCheckbox,
        createDateTime: createDateTime,
        createCalendar: createCalendar,
        createDialogCalendar: createDialogCalendar,
        getCheckedRadio: getCheckedRadio,
        checkRadioItem: checkRadioItem,
        cancelCheckedCheckbox:cancelCheckedCheckbox,
        cancelCheckedCheckbox2:cancelCheckedCheckbox2,
        createCalendarWidthRange:createCalendarWidthRange,
        createCalendarWidthRangeTime:createCalendarWidthRangeTime,
        createCalendarWidthRangeDate:createCalendarWidthRangeDate
    };
})();